﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BOM_STD_TECHNIC
// * CreateDate    : 2012/4/19 11:37:36
// * Creater       : CodeSmith
// * LastChangeDate: 2012/4/19 11:37:36
// * LastChanger   : 2012/4/19 11:37:36
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Bom
{
    /// <summary>
    /// BomStdTechnic
    /// </summary>
    public partial class BomStdTechnicBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomStdTechnicEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomStdTechnicEntity GetBomStdTechnic(BomStdTechnicEntity objBomStdTechnicEntity)
        {
            return DataAccess.SelectSingle<BomStdTechnicEntity>(objBomStdTechnicEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBomStdTechnicEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomStdTechnic(BomStdTechnicEntity objBomStdTechnicEntity)
        {
            return DataAccess.Insert(objBomStdTechnicEntity);
        }

        public static bool InsertBomStdTechnic(List<BomStdTechnicEntity> objBomStdTechnicEntityList)
        {
            return DataAccess.Insert(objBomStdTechnicEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomStdTechnicEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomStdTechnic(BomStdTechnicEntity objBomStdTechnicEntity)
        {
            return DataAccess.Update(objBomStdTechnicEntity);
        }

        public static bool UpdateBomStdTechnic(List<BomStdTechnicEntity> objBomStdTechnicEntityList)
        {
            return DataAccess.Update<BomStdTechnicEntity>(objBomStdTechnicEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomStdTechnicEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomStdTechnic(BomStdTechnicEntity objBomStdTechnicEntity)
        {
            return DataAccess.Delete(objBomStdTechnicEntity);
        }

        public static bool DeleteBomStdTechnic(List<BomStdTechnicEntity> objBomStdTechnicEntityList)
        {
            return DataAccess.Delete<BomStdTechnicEntity>(objBomStdTechnicEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomStdTechnicAll()
        {
            return DataAccess.SelectAllDataSet<BomStdTechnicEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomStdTechnicEntity> GetBomStdTechnicAllList()
        {
            return DataAccess.SelectAll<BomStdTechnicEntity>();
        }
        #endregion

        #region Functions
        public static DataTable GetDtAllByProductID(string sProductID)
        {
            string sql = @"select bst.*,pt.*,mci.ITEM_NAME,mwg.GROUP_NAME from BOM_STD_TECHNIC bst
left join PROD_TECHNIC pt on bst.STATION_ID=pt.TECHNIC_ID
left join MST_COMMON_ITEM mci on pt.STATION_TYPE=mci.ITEM_TYPE
left join MST_WORKING_GROUP mwg on pt.GROUP_ID=mwg.GROUP_ID
 where bst.product_id='" + sProductID + "' ";
            sql += "order by bst.SEQUENCE";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        public static bool SaveStdTechnic(MstProductEntity entity,DataTable dt)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    string sql = @"delete from BOM_STD_TECHNIC where product_id='" + entity .ProductId+ "'";
                    broker.ExecuteSQL(sql);
                    List<BomStdTechnicEntity> list = new List<BomStdTechnicEntity>();
                    int nSequence = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        BomStdTechnicEntity m = new BomStdTechnicEntity();
                        m.BomTechnicId = Guid.NewGuid().ToString();
                        m.ProductId = entity.ProductId;
                        m.StationId = dr["TECHNIC_ID"].ToString();
                        m.Sequence = nSequence;
                        m.Remark=dr["REMARK"].ToString();
                        if (string.IsNullOrEmpty(dr["STD_WORKING_TIME"].ToString()) == false)
                        {
                            m.StdWorkingTime = Convert.ToDecimal(dr["STD_WORKING_TIME"].ToString());
                        }
                        list.Add(m);
                        nSequence++;
                    }
                    DataAccess.Insert<BomStdTechnicEntity>(list, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }


}

